module.exports = function ($scope, $modalInstance, $resource, $modal, item, flag, toaster){
    

   

     /* 分页
     * ========================================= */
    $scope.maxSize = 5;             //最多显示多少个按钮
    $scope.bigCurrentPage = 1;      //当前页码
    $scope.itemsPerPage = 200         //每页显示几条
    init();
    function init(){
        $scope.url =  flag ?  'kong' : '/api/as/ic/createcity/updateArea'; 
        console.log($scope.url)
        console.log(flag)
        $scope.rulesinfo = flag ? {'flag': true} : angular.copy(item);
 
    }
    //保存
    $scope.save = function (){
        
        
        if($scope.url == 'kong'){
            console.log($scope.rulesinfo.path)
            if($scope.rulesinfo.path == 1){
                $scope.url = '/api/as/ic/createcity/createPath1';
            }else if($scope.rulesinfo.path == 2){
                if($scope.arealist1.length < 1){
                    toaster.error({title: '', body:'未查到上级数据无法创建。'});
                    return;
                }
                $scope.url = '/api/as/ic/createcity/createPath2';
            }else if($scope.rulesinfo.path == 3){
                if($scope.arealist1.length < 1){
                    toaster.error({title: '', body:'未查到上级数据无法创建。'});
                    return;
                }
                if($scope.arealist2.length < 1){
                    toaster.error({title: '', body:'未查到上级数据无法创建。'});
                    return;
                }
                $scope.url = '/api/as/ic/createcity/createPath3';
            }
        }
        console.log($scope.url)
        $resource( $scope.url , {}, {}).save($scope.rulesinfo, function(res){
            if(res.errcode === 0){
                toaster.success({title: '', body: '保存成功'});
                $modalInstance.close();
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }
   
    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    }


    //查询上级机构列表
    $scope.findarealist = function () {
        $modalInstance.dismiss('cancel');
    }

    $scope.findarealist1 = function (path){
        var para = {
            'path': '1',
            'pageNo':$scope.bigCurrentPage, 
            'pageSize':$scope.itemsPerPage,
        }
        $resource('/api/as/ic/createcity/findAreaList', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                $scope.arealist1 = res.data.results;
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    $scope.findarealist2 = function (areacode){
       
        var para = {
            'path': '2',
            'up_area_code': areacode,
            'pageNo':$scope.bigCurrentPage, 
            'pageSize':$scope.itemsPerPage,
        }
        console.log(para)
        $resource('/api/as/ic/createcity/findAreaList', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                $scope.arealist2 = res.data.results;
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    //
    $scope.changepath = function (path) {
        $scope.arealist1 = {};
       $scope.openpath = path;
       $scope.findarealist1(path);
    }

    //
    $scope.changepath1 = function (areacode) {
        $scope.arealist2 = {};
        if($scope.openpath == 3){
            console.log($scope.openpath)
            $scope.findarealist2(areacode);
        }
        $scope.rulesinfo.up_area_code = areacode;
     }

     $scope.changepath2 = function (areacode) {
        console.log(areacode)
        $scope.rulesinfo.up_area_code = areacode;
        console.log( $scope.rulesinfo.up_area_code)
     }
};